package com.base.annotation;


import java.lang.annotation.Annotation;
import java.lang.reflect.Method;

/**
 * 解析java注解
 *
 * @author innocent
 * @date 2018/12/4 17:13
 */
public class MyTest {
    public static void main(String[] args) {
        try {
            //使用类加载器，加载类
            Class<?> p = Class.forName("com.annotations.test.Person");

            Annotation[] declaredAnnotations;

            //找到方法上的注解
            Method[] methods = p.getMethods();
            for (Method method : methods) {
                //判断方法是否被指定注解注解
                if (method.isAnnotationPresent(MyAnnotation.class)) {
                    //拿到注解实例
                    MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
                    declaredAnnotations = method.getDeclaredAnnotations();
                    for (Annotation declaredAnnotation : declaredAnnotations) {
                        System.out.println("declaredAnnotation = " + declaredAnnotation.annotationType().getName());
                    }
                    System.out.println("annotation = " + annotation.name());
                }
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
}
